<?
include_once('conn.php');
include_once('include/func.php');
include_once('include/up_img_class.php');
include("admin_check.php") ;
if($admin[0][2]<>10 and $admin[0][2]<>9){exit('<script>alert("您没有权限"); window.history.back(-1);</script>');}
$last_id = $db->select("SELECT sid FROM `".tablepre."stock` order by sid desc");
$countsql = $last_id[0][0]+1;
for($i=0;$i<count($last_id);$i++){
	if($last_id[$i][0]==$countsql){$countsql=$countsql+1;}
	}


$countsql = $last_id[0][0]+1;
if(isset($_GET[add])){



		if($_FILES["picture"]["name"]<>null){
		$uploadpic = new uploadpic('upload/'.substr(filetime,0,8),$_FILES["picture"]);
		$uploadpic->createfolder();
		 $pic=$uploadpic->pic_name();
		 $pic_path='upload/'.$uploadpic->pic_folder().'/'.$pic; 
	}else{
		$pic_path=$_POST[pic_url];
		}
	$db->INSERT("INSERT INTO `".tablepre."stock` (`sid`,`name`,`tid`,`delete`) VALUES ('$countsql','".htmlspecialchars($_POST[name],ENT_QUOTES, 'UTF-8')."', '$_GET[sid]','0')");
	$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('model','$countsql', '".htmlspecialchars($_POST[model],ENT_QUOTES, 'UTF-8')."')");
	//缩略图
	$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('pic','$countsql', '$pic_path')");
	$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('url','$countsql', '$_POST[url]')");
	header("Location: $nowpage?ok&action=additem&sid=$_GET[sid]&tid=$_GET[tid]&nowid=$countsql");
	exit;
	}
//添加商品
if(isset($_GET[ch])){

		if($_FILES["picture"]["name"]<>null){
		$uploadpic = new uploadpic('upload/'.substr(filetime,0,8),$_FILES["picture"]);
		$uploadpic->createfolder();
		$pic=$uploadpic->pic_name();
		$pic_path='upload/'.$uploadpic->pic_folder().'/'.$pic;
		
 $db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='".$pic_path."' WHERE `sid`='".$_GET[nowid]."' and `type` = 'pic'");
	}elseif($_POST[pic_url]<>null){
		$db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='".$_POST[pic_url]."' WHERE `sid`='".$_GET[nowid]."' and `type` = 'pic'");
		}
	$db->UPDATE("UPDATE `".tablepre."stock` SET `name`='$_POST[name]' WHERE (`sid`='$_GET[nowid]')");
	$db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='".$_POST[url]."' WHERE `sid`='".$_GET[nowid]."' and `type` = 'url'");
	$db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='$_POST[model]' WHERE `sid`='$_GET[nowid]' and `type` = 'model'");
	
	//缩略图
	echo $_POST[url];
	header("Location: $nowpage?ok&action=additem&sid=$_GET[sid]&tid=$_GET[tid]&nowid=$_GET[nowid]");
	exit;
	}
//修改商品
if(isset($_GET[nowid])){
	$checkch = $db->select("SELECT * FROM `".tablepre."stock` where sid = '$_GET[nowid]'");
	}
	
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? echo systemname;?></title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<!-- jquery packed -->
	<script type="text/javascript" src="js/jquery-1.4.4.js"></script>
    <script type="text/javascript" src="js/dtree.js"></script>
	<!-- tableRowCheckboxToggle -->
	<script type="text/javascript" src="js/tableRowCheckboxToggle.js"></script>
     <script type="text/javascript"> 
    function fade() 
{ 
    //停留3秒后消失,在停留的时候,透明度为1.0什么都不做 
    $('<div id="add" class="quick-alert">操作成功</div>').insertAfter($("#example")).animate({opacity: 1.0}, 3000).fadeOut("slow",function(){ 
   //隐藏时把元素删除 
   $(this).remove(); 
}); 
} 
</script> 
        <script type="text/javascript">
            function myfun()
            {
                var cha1;
                cha1=document.myform.name.value;
                if(cha1=="")
                {
                    alert("标题不可为空!");
                    return false;
                }

			}
        </script>
        <link href="js/facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="js/facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox() 
    })
</script>
        <style>.thisstyle{ width:860px;}</style>
</head>
<body <? echo isset($_GET[ok])?'onload="fade();"':''; ?>>
<div id="wrap">
<div id="main" class="clearfix">
<div id="side">
  </div>
  <div id="head"><? include_once('admin_head.php');?></div><table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top" style="width:200px;"><div id="left"><?
  include_once('admin_menu.php');
  ?></div></td>
    <td align="left" valign="top">
<div id="example"> </div><? if(isset($_GET[action])){
	echo '<a href="?sid='.$_GET[sid].'&tid='.$_GET[tid].'"><img src="images/ttfjpg.php?t=继续在本类添加商品&bg=titlebg.jpg&z=18&x=5&y=22&r=0&b=0&g=0"  /></a>';
	}
	if($_GET[action]<>'add_attachment'){$rbg1='&r=255&b=0&g=0'; $rbg2='&r=0&b=0&g=0';}else{$rbg2='&r=255&b=0&g=0'; $rbg1='&r=0&b=0&g=0';}
?>
<img src="images/ttfjpg.php?t=<? if($_GET[action]=='additem'){echo '修改信息';}else{echo '第一步 添加商品';}?>&bg=titlebg.jpg&z=18&x=5&y=22<? echo $rbg1; ?>"  /><? if($_GET[action]=='additem'){echo '<a href="admin_item.php?iframe&sid='.$_GET[nowid].'" rel="facebox[.thisstyle]" >';}?><img src="images/ttfjpg.php?t=第二步 添加库存 <? if($_GET[action]=='additem'){echo '[点击]';}?>&bg=titlebg.jpg&z=18&x=5&y=22<? echo $rbg2; ?>"  /><? if($_GET[action]=='additem'){echo '</a>';}?>
<form  name="myform" enctype="multipart/form-data" method="post" action="<? if(isset($_GET[ok])){echo '?ch';}elseif(isset($_GET[getch])){echo '?ch';}else{echo '?add';} ?>&sid=<? echo $_GET[sid]; ?>&tid=<? echo $_GET[tid]; ?><? echo isset($_GET[nowid])?'&nowid='.$_GET[nowid]:''; ?>" onSubmit="return myfun()">
<? if($_GET[action]=='add_attachment'){  ?>
<? } else{}?>
  <table width="98%" border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td align="right" bgcolor="#F3F3F3" style="width:65px;">标题：</td>
      <td bgcolor="#F3F3F3"><input name="name" type="text" value="<? echo $checkch[0][1]; ?>" size="30"  /></td>
    </tr>
    <tr>
      <td align="right">型号：</td>
      <td><input type="text" name="model" value="<? echo attachment($con0=1,$con1=$checkch[0][0],$con2=model); ?>" /></td>
    </tr>

    <tr>
      <td align="right" bgcolor="#F3F3F3">图片：</td>
      <td bgcolor="#F3F3F3">
       <script language="javascript">
function inner_1()
{
document.getElementById('tabmenu').innerHTML="<input name=\"picture\" type=\"file\" />";
}
function inner_2()
{
document.getElementById('tabmenu').innerHTML="<input name=\"pic_url\" type=\"text\" size=\"40\" />要加上http:// 否则连接出错";
}
</script>
      本地图片<input name="radio" type="radio" id="radio1" value="1"   onclick="inner_1()"/>
      网络图片<input name="radio" type="radio" id="radio2" value="1"   onclick="inner_2()"/>
      <div id="tabmenu"></div>
      
        
        </td>
    </tr>
    <tr>
      <td align="right">商城连接：</td>
      <td><input name="url" type="text" size="40" value="<? echo attachment($con0=1,$con1=$checkch[0][0],$con2=url); ?>" />
        要加上http:// 否则连接出错</td>
    </tr>

    <tr>
      <td align="right">&nbsp;</td>
      <td><? $pic = attachment($con0=1,$con1=$checkch[0][0],$con2=pic);
	  if (preg_match ("/http:/i", $pic)) {
$loadpic = $pic;
} else {
    
	    $loadpic_tmp = explode("/", $pic );
	$loadpic = $loadpic_tmp[0].'/'.$loadpic_tmp[1].'/spic/'.$loadpic_tmp[2];
}

	  
	  echo $pic==null?'':'<a href="'.$pic.'" target="_blank"><img src="'.$loadpic.'"  /></a>';
	  ?>
      
      </td>
    </tr>

    <tr>
      <td align="right">&nbsp;</td>
      <td><input type="submit" name="button" value="<? echo !isset($_GET[action])?'添加商品':'修改'; ?>" /></td>
    </tr>
  </table>
</form></td>
  </tr>
</table>
  
  </div>
  </div>

<div id="footer">
<?
include_once('fooder.php');
?>
</div>
</BODY>
</HTML>


